from datetime import datetime, timedelta


class Monitor:
    def __init__(self, config_data):
        self.config_data = config_data
        self.car_min_speed = float(config_data['car_min_speed'])
        self.event = {}
        self.event_time = []

    def flow(self, data, last_data):
        # 流量变化较大
        flow = self.config_data["flow"]
        up_flow = last_data["up_flow_num"] - data["up_flow_num"]
        down_flow = last_data["down_flow_num"] - data["down_flow_num"]
        if up_flow > flow * last_data["up_flow_num"]:
            return True
        if down_flow > flow * last_data["down_flow_num"]:
            return True
        return False

    def flow_match(self, data, last_data):
        # 匹配流量变化较大
        flow = self.config_data["flow"]
        match = data["total_match"] - last_data["total_match"]
        if match > 10:
            if match > flow * last_data["total_match"]:
                return True
            if match > 20:
                return True
        return False

    def car_speed(self, data, last_data, vtype="1"):
        # 小客车速度骤降
        speed = self.config_data["speed"]
        if vtype not in data["type_speed_dict"].keys() or vtype not in last_data["type_speed_dict"].keys():
            return False
        current_speed = data["type_speed_dict"][vtype]["mean_speed"]
        last_speed = last_data["type_speed_dict"][vtype]["mean_speed"]
        speed_diff = last_speed - current_speed
        if speed_diff > speed * last_speed:
            return True
        if current_speed < self.car_min_speed:
            return True
        return False

    def car_slow_num(self, data, vtype="1"):
        # 小客车慢行数量过多
        slow_num = self.config_data["slow_num"]
        if vtype not in data["type_speed_dict"].keys():
            return False
        current_num = data["type_speed_dict"][vtype]["slow_level_count"]
        if current_num >= slow_num:
            return True
        return False

    def flow_empty(self, data):
        up_flow = data["up_flow_num"]
        down_flow = data["down_flow_num"]
        if up_flow == 0 and down_flow == 0:
            return True
        return False

    def pass_rate(self, data, vtype="1"):
        pass_rate = self.config_data["pass_rate"]
        if vtype not in data["type_pass_rate_dict"].keys():
            return False
        car_pass_rate = data["type_pass_rate_dict"][vtype]
        current_pass_rate = int(car_pass_rate["vaild_count"] / car_pass_rate["count"] * 100)
        if current_pass_rate < pass_rate:
            return True
        return False

    def run(self, data, last_data):
        if last_data is not None:
            event_time = data["time"]
            flow_flag = self.flow(data, last_data)
            speed_flag = self.car_speed(data, last_data)
            slow_flag = self.car_slow_num(data)
            flow_empty_flag = self.flow_empty(data)
            pass_flag = self.pass_rate(data)
            if flow_empty_flag:
                content = "车流量为零" + event_time.strftime(self.config_data["format_str"])
                return content
            if speed_flag:
                if pass_flag:
                    content = "车速过低，通过率过低" + event_time.strftime(self.config_data["format_str"])
                    return content
                else:
                    content = "车速过低" + event_time.strftime(self.config_data["format_str"])
                    return content
            if slow_flag:
                content = "慢行车辆过多" + event_time.strftime(self.config_data["format_str"])
                return content
        return None


if __name__ == '__main__':
    print("2222222222222")
